<template>
	<div class="update">
		<el-dialog :title="'发现新版本 V'+ dialog.version" :visible.sync="dialog.show" width="500px">
			<div class="dialog-center">
				<ul class="ul">
					<li v-for="(item,index) in dialog.list.li" :key="index" class="li">
						<div style="display: flex;">
							<el-tag size="small" :type="item.type" style="margin-right: 5px;">{{item.typeInfo}}</el-tag>
							<div style="color: #888;display: inline-block;">{{item.text}}</div>
						</div>
					</li>
				</ul>
				<div v-if="dialog.showPro" class="progress">
					<div class="p-title">更新进度：（{{$lizhao.lzByte.lzByte(dialog.progress.transferred)}}/{{$lizhao.lzByte.lzByte(dialog.progress.total)}}）下载速度：{{$lizhao.lzByte.lzByte(dialog.progress.bytesPerSecond)||'读取中...'}}/s</div>
					<el-progress :text-inside="true" :stroke-width="22" :percentage="Math.ceil(dialog.progress.percent)"></el-progress>
				</div>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button :size="size" @click="dialog.show = false">{{dialog.startDow?'后台 下载':'以后 再说'}}</el-button>
				<el-button :size="size" type="success" :loading="dialog.loading" @click="InstallUpdates" v-if="dialog.progress.transferred===dialog.progress.total">安装
					更新</el-button>
				<el-button :size="size" :disabled="dialog.showPro" type="primary" @click="sendUpdate" v-else>下载 更新</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	let that
	import bus from "/static/js/bus.js";
	import {
		mapGetters
	} from 'vuex'
	import {
		ipcRenderer
	} from "electron";
	export default {
		computed: {
			...mapGetters(['size'])
		},
		components: {},
		name: 'Home',
		data() {
			return {
				dialog: {
					show: false,
					list: [],
					progress: {
						percent: 0,//进度
						total: 0,//总大小
						transferred:1,//已下载
					},
					showPro: false,
					loading: false,
					startDow: false, //是否已经开始下载
					version:''
				},
				
			}
		},
		mounted() {
			that = this
			//检测更新
			that.updateEle()
		},
		methods: {
			//安装更新
			InstallUpdates() {
				ipcRenderer.send("isUpdateNow");
			},
			//下载 更新
			sendUpdate() {
				that.dialog.showPro = true
				that.dialog.loading = false
				that.dialog.startDow = true
				ipcRenderer.send("downloadUpdate");
			},
			updateEle() {
				that.GLOBAL.doGet('http://117.50.31.205:8099/electron/update.json', {}).then(res => {
					if (res.code == 0) {
						that.dialog.list = res.data[0]
					}
				})
				ipcRenderer.on("sendToRanderMessage", (event, text) => {
					if (text.code == 'update-available') { //当前有版本更新
						that.dialog.show = true
						that.dialog.version=text.data.version
					}
					bus.$emit('topActiveInfo', {
						action: 'drawLog',
						data: '检测更新: ' + JSON.stringify(text)
					})
				});
				//注意：“downloadProgress”事件可能存在无法触发的问题，只需要限制一下下载网速就好了
				ipcRenderer.on("downloadProgress", (event, progressObj) => {
					that.dialog.progress = progressObj
					bus.$emit('topActiveInfo', {
						action: 'drawLog',
						data: '下载更新: ' + JSON.stringify(progressObj)
					})
				});
				//下载完成
				ipcRenderer.on("isUpdateNow", () => {
					that.$confirm('资源已下载，是否立即安装更新？', '提示', {
						confirmButtonText: '安装',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						that.InstallUpdates()
					}).catch(() => {
					});
					bus.$emit('topActiveInfo', {
						action: 'drawLog',
						data: '下载完成: '
					})
				});
				ipcRenderer.send("checkForUpdate");
			},
		}
	}
</script>
<style scoped="scoped" lang="scss">
	.update {
		.dialog-center {
			padding: 0 20px;

			.ul {
				.li {
					margin-bottom: 5px;
				}
			}

			.progress {
				.p-title {
					margin-bottom: 10px;
				}
			}
		}
	}
</style>
